package com.shengruan.user.action;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shengruan.admin.jdbc.UserJdbc;
import com.shengruan.admin.vo.User;
import com.shengruan.common.Constants;
import com.shengruan.framework.action.ActionSupport;
import com.shengruan.framework.util.AuthFactory;

public class UserManageAction extends ActionSupport {

    Logger logger = LoggerFactory.getLogger(getClass());

    private UserJdbc userJdbc = (UserJdbc) this.getJdbc(UserJdbc.class);


    /**
     * 显示用户个人信息
     *
     * @throws Exception
     */
    public void doLoadPersonalInfo() throws Exception {
        this.forward("/user/user-personalInfo.jsp");
    }


    /**
     * check by mobile
     *
     * @throws Exception
     */
    public void checkMobile() throws Exception {
        String mobile = this.input("mobile");
        User sessionUser = (User) this.request.getSession().getAttribute(Constants.USER);
        User user = userJdbc.getByMobile(mobile, sessionUser.getId().toString());
        if (null == user) {
            this.print("true");
        } else {
            this.print("false");
        }
    }

    /**
     * @throws Exception
     */
    public void checkEmail() throws Exception {
        String email = this.input("email");
        User sessionUser = (User) this.request.getSession().getAttribute(Constants.USER);
        User user = userJdbc.getByEmail(email, sessionUser.getId().toString());
        if (null == user) {
            this.print("true");
        } else {
            this.print("false");
        }
    }


    /**
     * 更新个人信息
     *
     * @return
     * @throws Exception
     */
    public void doPersonalInfo() throws Exception {
        String realName = this.input("realName");
        String email = this.input("email");
        String mobile = this.input("mobile");
        String sex = this.input("sex");
        String options = this.input("options");

        User user = (User) this.request.getSession().getAttribute(Constants.USER);
        user.setRealName(StringUtils.trim(realName));
        user.setEmail(email);
        user.setMobile(mobile);
        user.setSex(sex);
        user.setOptions(StringUtils.trim(options));

        userJdbc.update(user);
        this.setSession(Constants.USER, user);

        this.setRequest(Constants.MESSAGE, "更新个人信息成功.");
        this.redirect("/user/user-doLoadPersonalInfo.ac");
    }


    /**
     * 加载修改密码界面
     *
     * @throws Exception
     */
    public void doLoadChangePassword() throws Exception {
        this.forward("/user/user-changePassword.jsp");
    }

    /**
     * 用户修改密码
     *
     * @throws Exception
     */
    public void doChangePassword() throws Exception {
        String passwordOld = this.input("passwordOld");
        String password = this.input("password");
        String password_confirm = this.input("password_confirm");


        if (StringUtils.isEmpty(passwordOld)) {
            this.setRequest(Constants.MESSAGE, "请输入旧密码");
            this.forward("/user/user-changePassword.jsp");
            return;
        }
        User user = (User) this.request.getSession().getAttribute(Constants.USER);
        AuthFactory.passwordKey = Constants.passwordKey;
        if (!AuthFactory.decryptPassword(user.getPassword()).equalsIgnoreCase(passwordOld)) {
            this.setRequest(Constants.MESSAGE, "旧密码不正确请重新输入");
            this.forward("/user/user-changePassword.jsp");
            return;
        }

        if (StringUtils.isEmpty(password)) {
            this.setRequest(Constants.MESSAGE, "请输入新密码");
            this.forward("/user/user-changePassword.jsp");
            return;
        }

        if (StringUtils.isEmpty(password_confirm)) {
            this.setRequest(Constants.MESSAGE, "请输入确认密码");
            this.forward("/user/user-changePassword.jsp");
            return;
        }

        if (!password.equals(password_confirm)) {
            this.setRequest(Constants.MESSAGE, "两次输入的密码必须相同");
            this.forward("/user/user-changePassword.jsp");
            return;
        }

        AuthFactory.passwordKey = Constants.passwordKey;
        String encryptPassword = AuthFactory.encryptPassword(password);
        userJdbc.changePassword(user.getId().toString(), encryptPassword);

        user.setPassword(encryptPassword);
        this.setSession(Constants.USER, user);

        this.setRequest(Constants.MESSAGE, "修改密码成功.");
        this.forward("/user/user-changePassword.jsp");
    }
}
